package com.fengye.redisson.star.controller;

import com.fengye.redisson.star.dto.PraiseDTO;
import com.fengye.redisson.star.response.BaseResponse;
import com.fengye.redisson.star.response.StatusCode;
import com.fengye.redisson.star.service.PraiseService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;

/**
 *
 * @author sky
 * @date 2021年06月16日 1:39 下午
 */
@RestController
@Slf4j
@RequestMapping("/praise")
public class PraiseController {

    @Autowired
    PraiseService praiseService;

    /**
     * 用户点赞
     * @param dto
     */
    @PostMapping(value = "/add", consumes = MediaType.APPLICATION_JSON_VALUE)
    public BaseResponse addPraise(@RequestBody @Validated PraiseDTO dto, BindingResult result) {

        if (result.hasErrors()) {
            return new BaseResponse(StatusCode.InvalidParams);
        }

        BaseResponse response = new BaseResponse(StatusCode.Success);

        HashMap<String, Object> resultMap = new HashMap<>();
        try {
            // 点赞 - 不加锁
            praiseService.addPraise(dto);

            // 点赞 - 加锁
            // praiseService.addPraiseWithLock(dto);

            // 获取博客的点赞数
            Long total = praiseService.getBlogPraiseTotal(dto.getBlogId());

            resultMap.put("praiseTotal", total);
        } catch (Exception e) {
            e.printStackTrace();
        }

        response.setData(resultMap);
        return response;

    }

    /**
     * 用户点赞
     * @param dto
     */
    @PostMapping(value = "/add/lock", consumes = MediaType.APPLICATION_JSON_VALUE)
    public BaseResponse addPraiseWithLock(@RequestBody @Validated PraiseDTO dto, BindingResult result) {

        if (result.hasErrors()) {
            return new BaseResponse(StatusCode.InvalidParams);
        }

        BaseResponse response = new BaseResponse(StatusCode.Success);

        HashMap<String, Object> resultMap = new HashMap<>();
        try {
            // 点赞 - 不加锁
            // praiseService.addPraise(dto);

            // 点赞 - 加锁
            praiseService.addPraiseWithLock(dto);

            // 获取博客的点赞数
            Long total = praiseService.getBlogPraiseTotal(dto.getBlogId());

            resultMap.put("praiseTotal", total);
        } catch (Exception e) {
            e.printStackTrace();
        }

        response.setData(resultMap);
        return response;

    }

    /**
     * 用户点赞取消
     * @param dto
     */
    @PostMapping(value = "/cancel", consumes = MediaType.APPLICATION_JSON_VALUE)
    public BaseResponse cancelPraise(@RequestBody @Validated PraiseDTO dto, BindingResult result) {

        if (result.hasErrors()) {
            return new BaseResponse(StatusCode.InvalidParams);
        }

        BaseResponse response = new BaseResponse(StatusCode.Success);

        HashMap<String, Object> resultMap = new HashMap<>();
        try {
            praiseService.cancelPraise(dto);

            // 获取博客的点赞数
            Long total = praiseService.getBlogPraiseTotal(dto.getBlogId());

            resultMap.put("praiseTotal", total);
        } catch (Exception e) {
            e.printStackTrace();
        }

        response.setData(resultMap);
        return response;

    }

    @GetMapping("/rank")
    public BaseResponse getRankPraise() {
        BaseResponse response = new BaseResponse(StatusCode.Success);

        try {
            response.setData(praiseService.getRankWithRedisson());
        }catch (Exception e){
            response = new BaseResponse(StatusCode.Fail.getCode(), e.getMessage());
        }
        return response;
    }
}
